<?
/* Copyright (c) 2010 Skobkin.ru

 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:

 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
?>

<html>
<head>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=Windows-1251"> 
<title>e2 to WordPress export</title>
</head>
<body>
<?
/* Instructions:
 * Import your e2Notes table in WP database, configure this
 * script and run 
 */

// Settings
// e2
$e2_host = "localost";
$e2_user = "root";
$e2_password = "";
define("e2_db", "e2"); // strange bug with var

$e2_table = "e2Notes";

// WP
$wp_table = "wp_posts";

// Import constants
$post_author = "1"; // Author id in WP database
$post_status = "publish"; // Post status: "publish", "trash", "inherit"
$post_parent = "0"; // Parent post if revision, 0 if single post.
$ping_status = "open"; // Allow ping
$post_type = "post";

function e2_connect_db()
{
	$id = mysql_connect ("$e2_host", "$e2_user", "$e2_password") or die("MySQL connect error: " .mysql_error());
    mysql_select_db (e2_db) or die("DB Error: ".mysql_error());
	mysql_query("set names 'cp1251'", $id);
}

function close_db()
{
	mysql_close();
}

function change_syntax($text) // Changes all post text using change_tag()
{
		$text = preg_replace("/\(\(img\s(.*?)\)\)/","<img src=\"$1\">",$text); // <img>
		$text = preg_replace("/\(\((http[^\s]*)\s+(.*?)\)\)/","<a href=\"$1\">$2</a>",$text); // <a>title</a>
		$text = preg_replace("/\(\((http[^\s]*)\)\)/","<a href=\"$1\">$1</a>",$text); // <a>href</a>
		$text = preg_replace("/\(\(html\s(.*?)\)\)/","$1",$text); // HTML
		$text = preg_replace("/\(\(cut\s(.*?)\)\)/","[cut text=\"$1\"]",$text); // <cut>
		$text = preg_replace("/\(\(ljuser\s(.*?)\)\)/","<a href=\"http://$1.livejournal.com\" target=\"_blank\" >$1</a>",$text); // <a href="%user%.livejournal.com>
		
		// formating
		$text = preg_replace("/\*\*(.*?)\*\*/","<b>$1</b>",$text); // <b>
		$text = preg_replace("/!!(.*?)!!/","<blockquote>$1</blockquote>",$text); // <blockquote> don't work O_o
		$text = preg_replace("/\-\-(.*?)\-\-/","<span style=\"text-decoration: line-through;\">$1</span>",$text); // <span style="text-decoration: line-through;">
	return $text;
}

// ========================

e2_connect_db();

$equery = "SELECT * FROM $e2_table"; 
$res = mysql_query($equery) or die(mysql_error());
$number = mysql_num_rows($res); 
if ($number == 0) 
{ 
  echo "<CENTER><P>No posts found.</CENTER>"; 
} 
else 
{
	echo "<CENTER><P>$number posts found!</CENTER><br><br>"; 
 
	while ($row=mysql_fetch_array($res)) { 
    // Take data for export
	$post_title = addslashes($row['Title']);
	$post_text = addslashes($row['Text']);
	$post_published = $row['IsPublished'];
	$post_commentable = $row['IsCommentable'];
	
	echo "exporting ".$row['ID']." post<br>";
	$post_text = change_syntax($post_text);
	//echo "post:<br>".$post_text."<br>";
	
	// Making query
	$iquery = "INSERT INTO $wp_table VALUES(
		'',
		'".$post_author."',
		'".date("Y-m-d G:i:s", $row['Stamp'])."',
		'".date("Y-m-d G:i:s", $row['Stamp'])."',
		'".$post_text."',
		'".$post_title."',
		'',
		'".$post_status."',
		'".($row['IsCommentable']=="1" ? "open" : "closed")."',
		'".$ping_status."',
		'',
		'',
		'',
		'',
		'".date("Y-m-d G:i:s", $row['LastModified'])."',
		'".date("Y-m-d G:i:s", $row['LastModified'])."',
		'',
		'".$post_parent."',
		'',
		'0',
		'".$post_type."',
		'',
		'0'
		)";
		
	// exporting
	mysql_query($iquery) or die(mysql_error());
	}
};

close_db();
?>

</body>
</html>